package com.wxr.springcloud.controller;

import com.wxr.springcloud.pojo.Dept;
import com.wxr.springcloud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private DiscoveryClient client;


    @RequestMapping("/add")
    @ResponseBody
    public boolean addDept(Dept dept){
        return deptService.addDept(dept);
    }

    /**
     * restful 风格
     * */
    @RequestMapping("/get/{id}")
    @ResponseBody
    public Dept get(@PathVariable("id")Long id){
        return deptService.queryById(id);
    }


    @RequestMapping("/list")
    @ResponseBody
    public List<Dept> queryAll(){
        return deptService.queryAll();
    }

    //注册进来的微服务~ 获取一些消息
    @RequestMapping("/discovery")
    @ResponseBody
    public Object disCovery(){
        //获取到所有微服务信息
        List<String> services = client.getServices();
        System.out.println("discovery=>services"+services);
        //得到一个具体的微服务信息
        List<ServiceInstance> instances = client.getInstances("SPRINGCLOUD-PROVIDER-DEPT");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getHost()+"\t"+instance.getServiceId()+"\t"+instance.getMetadata()+"\t"+instance.getPort()+"\t"+instance.getUri());
        }
        return this.client;
    }

}
